﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using TimKen.App;
using TimKen.DataEntity;
using System.Collections.Generic;

namespace TimKen.Web.Admin.PAttribute
{
    public partial class AddOpToType : TimKen.App.AdminPage
    {
        private TimKen.BussLogic.PCategoryManager Categorym = new TimKen.BussLogic.PCategoryManager();
        public override string ActionCode
        {
            get
            {
                return "89";
            }
        }
        public string CategoryID = "";
        string fieldType = "-1", attrName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryID = Request.QueryString["CategoryID"] == null ? "" : Request.QueryString["CategoryID"].ToString();
            if (TypeParse.IsNumeric(CategoryID))
            {
                if (!this.IsPostBack)
                {

                    BindGrid();
                    BindResources();
                }
            }
            else
            {
                Alert(GetLangString("InvalidCategoryID"));
            }
        }

        public void BindGrid()
        {
            DataSet ds = Categorym.GetOptionNotInCategory(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, CategoryID, this.LanguageID.ToString(), fieldType, attrName);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["totle"]);
            grdOption.DataSource = ds.Tables[0].DefaultView;
            grdOption.DataBind();

        }

        protected void btnAddToCategroy_Click(object sender, EventArgs e)
        {
            string chkids = Request.Form["POption"];
            string chkindexids = Request.Form["POptionIndex"];
            AddOptionToCategory(chkids, chkindexids);
            BindGrid();
        }

        protected void btnAddToCategroyComplete_Click(object sender, EventArgs e)
        {
            string chkids = Request.Form["POption"];
            string chkindexids = Request.Form["POptionIndex"];
            AddOptionToCategory(chkids, chkindexids);
            Response.Redirect("CategoryOption_list.aspx?Categoryid=" + CategoryID);
        }

        private void AddOptionToCategory(string chkids, string chkindexids)
        {
            IList<POptionRelPCategory> list = new List<POptionRelPCategory>();
            if (string.IsNullOrEmpty(chkids))
            {
                Alert(GetLangString("ChooseNoOption"));
            }
            else
            {
                string[] ids = chkids.Split(',');
                string[] indexids = new string[] { "!@!YHH" };
                if (!string.IsNullOrEmpty(chkindexids))
                {
                    indexids = chkindexids.Split(',');
                }
                foreach (string id in ids)
                {
                    POptionRelPCategory po = new POptionRelPCategory();
                    po.OptionID = Convert.ToInt32(id);
                    po.CategoryID = Convert.ToInt32(CategoryID);

                  //  po.AttributeIndex = indexids.Contains<string>(id);

                    list.Add(po);
                }
                Categorym.AddOptionToCategory(list);
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            fieldType = dplAttrType.SelectedValue;
            attrName = txtAttrName.Text.Trim();
            BindGrid();
        }

        public void BindResources()
        {
            btnAddToCategroy.Text = GetLangString("AddToCategroy");
            btnAddToCategroyComplete.Text = GetLangString("AddToCategroyComplete");
            ListItem itemsAll = new ListItem(GetLangString("FieldType_All"), "-1");
            itemsAll.Selected = true;
            dplAttrType.Items.Add(itemsAll);
            dplAttrType.Items.Add(new ListItem(GetLangString("FieldType_TextBox"), "0"));
            dplAttrType.Items.Add(new ListItem(GetLangString("FieldType_TextArea"), "1"));
            dplAttrType.Items.Add(new ListItem(GetLangString("FieldType_Select"), "2"));
            btFilter.Text = GetLangString("Search");
            btNoFilter.Text = GetLangString("ClearSearchCondition");
        }

        protected void btFilter_Click(object sender, EventArgs e)
        {
            fieldType = dplAttrType.SelectedValue;
            attrName = txtAttrName.Text.Trim();
            AspNetPager1.CurrentPageIndex = 1;
            BindGrid();
        }

        protected void btNoFilter_Click(object sender, EventArgs e)
        {
            dplAttrType.SelectedValue = "-1";
            txtAttrName.Text = "";
            attrName = "";
            fieldType = "-1";
            AspNetPager1.CurrentPageIndex = 1;
            BindGrid();
        }


    }
}
